package com.starblink.android.basic.sensorsdata

import android.os.Looper
import android.view.View
import com.starblink.android.basic.R
import com.starblink.android.basic.sensorsdata.exposure.ViewExposeDelegate
import com.starblink.android.basic.sensorsdata.exposure.config.ExposeConfig
import com.starblink.android.basic.sensorsdata.room.entity.SkAntEntity
import com.starblink.android.basic.sensorsdata.spm.event.TrackEvent
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

/**
 * ！！！请勿随便移动包名，插桩代码暂时写死了
 */
object SkViewTracker {

    private const val TAG = "SkViewTracker"

    init {
        MainScope().launch {
            Looper.myQueue().addIdleHandler {
                //Log.d(TAG, "-->> queueIdle")
                true
            }
        }
    }

    fun trackView(
        view: View,
        data: SkAntEntity?,
        showRatio: Float = ExposeConfig.showRatio,
        exposeTime: Float = ExposeConfig.exposeTime,
        onlyHit: Boolean = false,
    ) {
        view.let {
            it.setTag(R.id.view_track_data, data)
//            it.tag = data
            it.setTag(R.id.view_track_hit, onlyHit)
            if (!onlyHit) {
                data?.event = TrackEvent.elementExposure.value
//                if (showRatio <= 0.01F && exposeTime <= 10F) {
//                    // 一般情况下 View调用此方法时直接上报 与ios一致
//                    if(data !=null){
//                        SpmTrackHandler.addSingleTrackData(data)
//                    }
//                } else {
                    if (it.getTag(R.id.view_exposer_tag) == null) {
                        val delegate = ViewExposeDelegate(it, showRatio, exposeTime)
                        it.setTag(R.id.view_exposer_tag, delegate)
                    }
//                }
            } else {
                data?.event = TrackEvent.elementClick.value
            }
        }
    }

    /**
     * 点击事件发送
     */
    @JvmStatic
    fun fireEvent(view: View) {
        var tagData = view.getTrackData()
        val onlyHit = view.getTag(R.id.view_track_hit)
        if (tagData == null) {
            tagData = view.tag
        }
        if (tagData is SkAntEntity) {
            if (onlyHit == true) {
                tagData.event = TrackEvent.elementClick.value
            } else {
                tagData = tagData.copy().apply {
                    event = TrackEvent.elementClick.value
                }
            }
            SpmTrackHandler.addSingleTrackData(tagData)
        }
//        if (BuildConfig.DEBUG && tagData is SkAntEntity) {
//            //Toast.makeText(view.context, tagData.event, Toast.LENGTH_SHORT).show()
//            Log.d("fireEvent", "-->> $tagData")
//        }
    }

    fun View.getTrackData(): Any? {
        var trackData: Any? = null
        if (tag is SkAntEntity) {
            trackData = tag
        }
        if (trackData == null) {
            trackData = getTag(R.id.view_track_data)
        }
        return trackData
    }

}